<template>
  <div>
    <!-- 订单查看-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"
               :labelWidth="110"
               @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="orderDetailQuery"
        :isSelectColumn="true"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" @click="exportHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose
            :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="detailCloseHandler"/>
    </Drawer>
  </div>
</template>
<script>
import {exporturl, getList} from '@/api/dealerSaleP/OrderManagement/OrderDetailQuery';
import detailModel from './detailModel';
import {dictionary} from '@/utils/dictMixin';
import Cookies from "js-cookie";

export default {
  components: {
    detailModel
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        dealerCode: Cookies.get('ownerCode')
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      detailVisible: false,
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2105')
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'vehicleUse',
          placeholder: '请选择',
          itemList: this.createDictList('2102')
        },
        {
          type: 'SELECT',
          label: '订单明细状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2109')
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '车系名称',
          fieldName: 'seriesId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车型名称',
          fieldName: 'modelId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '配置名称',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '选装名称',
          fieldName: 'optionId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '内饰',
          fieldName: 'trimId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '外饰',
          fieldName: 'colorId',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '结算订单编号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{this.createDictText(p.row.emergencyType, '2101')}</span>;
          }
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2105'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆用途',
          dataIndex: 'vehicleUse',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{this.createDictText(p.row.vehicleUse, '2102')}</span>;
          }
        },
        {
          title: '订单明细状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '2109')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'accountId',
          sorter: true,
          filter: true,
          filterType: 'input',
          // editable: true,
          // editType: 'select',
          // editRequired: true,
          // rules: [{required: true, message: '请填写正整数', trigger: 'blur'}],
          // render: p => {
          //   return (
          //     <el-select
          //       size="mini"
          //       v-model={p.row.accountId}
          //       required
          //       placeholder="请选择"
          //       onChange={value => {
          //         p.row.accountId = value;
          //         p.row.accountTypes.map((item) => {
          //           if (item.accountId === value) {
          //             p.row.ownFund = x.ownFund;
          //             p.row.tripFund = x.tripFund;
          //           }
          //         })
          //       }}
          //     >
          //       {p.row.accountTypes.map(item => (
          //         <el-option key={item.accountId} label={item.accountTypeName} value={item.accountId}/>
          //       ))}{' '}
          //     </el-select>
          //   );
          // }
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '采购价格',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 100
        },
        {
          title: '现金折让',
          dataIndex: 'discountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          }
        },
        {
          title: '返利池返利',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          }
        },
        {
          title: '收车地址',
          dataIndex: 'addressId',
          sorter: true,
          filter: true,
          filterType: 'input',
          // render: (p) => {
          //   return (
          //     <el-select
          //       size="mini"
          //       v-model={p.row.addressId}
          //       required
          //       placeholder="请选择"
          //       onChange={(value) => {
          //         p.row.addressId = value;
          //         this.orderAddressOnChange(p.row);
          //       }}
          //     >
          //       {p.row.addressList.map((item) => (
          //         <el-option key={item.addressId} label={item.detaildAddress} value={item.addressId}/>
          //       ))}
          //     </el-select>
          //   );
          // },
        },
        {
          title: '送达城市',
          dataIndex: 'orderCity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '业务审核时间',
          dataIndex: 'confirmDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发运时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '到店交付时间',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    async exportHandle(type) {
      if (type === 1) {
        let json = Object.assign({}, {CODE: 'CODE_0000011'}, this.fetchParams);
        let res = await exporturl(json);

        if (res.resultCode === 200) {
          this.$router.push('/admin/auth/oem/downloadCenter');
        }
      }
    },
    detailHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    detailCloseHandler() {
      this.detailVisible = false;
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    resetChange(val) {
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
  }
};
</script>
